﻿$(document).ready(function () {
    $('#ribbon').ribbon();

    $("#check_all").click(function () {
        var checked_status = this.checked;
        $("[id$=chkSelect]").each(function () {
            this.checked = checked_status;
        });
    });

    $(".multidelete").click(function () {
        var n = $("[id$=chkSelect]:checked").length;
        var selectedIds = $("[id$=chkSelect]:checked");
        if (n == 0) {
            alert('Vui lòng chọn dòng cần xóa!');
            return false;
        }
        else {
            var ok = confirm('CẢNH BÁO: Bạn có chắc chắn muốn xóa dòng được chọn?');
            return ok;           
        }
    });

    BlockUI("dvGrid");
    $.blockUI.defaults.css = {};

    if ($('.datepicker').length > 0) {
        $('.datepicker').datepicker();
    }

    var startDate = new Date(2012, 1, 20);
    var endDate = new Date(2012, 1, 25);
    $('[id$=txtTuNgay]').datepicker().on('changeDate', function (ev) {
        startDate = ev.date.valueOf();
    });



    $('[id$=txtDenNgay]').datepicker()
				.on('changeDate', function (ev) {
				    if (ev.date.valueOf() < startDate.valueOf()) {
				        $('#alert').show().find('strong').text('Ngày kết thúc phải lớn hơn ngày bắt đầu');
				    } else {
				        $('#alert').hide();
				    }
				    $('[id$=txtDenNgay]').datepicker('hide');
				});
});

function BlockUI(elementID) {
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_beginRequest(function () {
        $("#" + elementID).block({
            message: '<table align = "center"><tr><td>' +
             '<img src="/images/loadingAnim.gif"/></td></tr></table>',
            css: {},
            overlayCSS: {
                backgroundColor: '#000000', opacity: 0.6, border: '3px solid #63B2EB'
            }
        });
    });
    prm.add_endRequest(function () {
        $("#" + elementID).unblock();
    });
}
function responseRedirect(url) {
    location.href = url;
}

$(document).ready(function () {
    var thumb = jQuery("img#imgthumb");
    var txtPicture = jQuery("[id$=txtHinhDaiDien]");
    txtPicture.blur(function () {
        thumb.attr('src', txtPicture.val());
    });
});
$(document).ready(function () {
    if ($("#choosefile").length > 0) {
        doUpload();
    }
});
function doUpload() {
    var action = jQuery("#choosefile");
    var preview = jQuery("img#imgthumb");
    var output = jQuery("[id$=txtHinhDaiDien]");
    new AjaxUpload(action, {
        action: '/FileUpload.axd',
        name: 'myFile',
        data: { 'folder': '/Images/anhcanbo/' },
        multiple: true,
        autoSubmit: true,
        onSubmit: function (file, extension) {
            if (!(extension && /^(jpg|png|jpeg|gif|bmp|JPG|PNG|JPEG|BMP|GIF)$/.test(extension))) {

                alert('Only JPG, PNG or GIF files are allowed');
                return false;
            }

            preview.attr('src', '/images/loadingAnim.gif');

            output.val('Loading...');
            output.attr('disabled', true);
            this.disable();

        },
        onComplete: function (file, response) {
            preview.load(function () {
                preview.unbind();
            });
            output.attr('disabled', false);
            var resp = response;
            resp = jQuery(resp).html();
            preview.attr('src', resp);
            this.enable();

            output.val(resp);

            return true;
        }
    });
}

function deleteCanBoChucVu(maCanBo, maChucVu) {
    var data = { "maCanBo": maCanBo, "maChucVu": maChucVu };
    $.ajax({
        url: "/WebService.asmx/Delete_CanBoChucVu",
        data: JSON.stringify(data),
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success:function(result){
            $('a#achucvu_' + maChucVu).closest('li').remove();
        },
        error: function (errormessage) {
            alert(errormessage.responseText);
        }
    });
    return false;
}
function deleteCanBoKhenThuong(maCanBo, maKhenThuong) {
    var data = { "maCanBo": maCanBo, "maKhenThuong": maKhenThuong };
    $.ajax({
        url: "/WebService.asmx/Delete_CanBoKhenThuong",
        data: JSON.stringify(data),
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            $('a#akhenthuong_' + maKhenThuong).closest('li').remove();
        },
        error: function (errormessage) {
            alert(errormessage.responseText);
        }
    });
    return false;
}
function deleteCanBoKyLuat(maCanBo, maKyLuat) {
    var data = { "maCanBo": maCanBo, "maKyLuat": maKyLuat };
    $.ajax({
        url: "/WebService.asmx/Delete_CanBoKyLuat",
        data: JSON.stringify(data),
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            $('a#akyluat_' + maKyLuat).closest('li').remove();
        },
        error: function (errormessage) {
            alert(errormessage.responseText);
        }
    });
    return false;
}
function logout() {
    var data = {  };
    $.ajax({
        url: "/WebService.asmx/Logout",
        data: JSON.stringify(data),
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            responseRedirect('/');
        },
        error: function (errormessage) {
            alert(errormessage.responseText);
        }
    });
    return false;
}